package com.example.idachuappone.activie;

import android.app.Dialog;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.index.welcomentity.Events;
import com.example.idachuappone.utils.BitmapUtilHelper;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.MainToast;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class ActiveDetailActivity extends BaseActivity {
	private Button btn_back;
	private BitmapUtils bu;
	private Dialog dialog;
	private Events events;
	private HttpUtils httpUtils;
	private ImageView rl_active_head;
	private TextView tv_active_description;
	private TextView tv_active_title;
	private String url_content;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_active_detail);
		this.httpUtils = new HttpUtils();
		this.bu = BitmapUtilHelper.createBitmapUtil(this);
		this.events = (Events) getIntent().getExtras().get("events");
		this.dialog = ComUtil.createCusProgressSmallBlackBgDialog(this);
		initView();
	}

	private void initData() {
		this.bu.display(this.rl_active_head, this.events.getHead());
		this.tv_active_description.setText(Html.fromHtml(this.url_content));
		this.tv_active_title.setText(this.events.getTitle());
	}

	private void initView() {
		this.btn_back = (Button) findViewById(R.id.btn_back);
		this.btn_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ActiveDetailActivity.this.finish();
			}
		});
		this.rl_active_head = (ImageView) findViewById(R.id.rl_active_head);
		this.tv_active_title = (TextView) findViewById(R.id.tv_active_title);
		this.tv_active_description = (TextView) findViewById(R.id.tv_active_description);
		this.httpUtils.send(HttpMethod.GET, this.events.getUrl(), new RequestCallBack<String>() {
			@Override
			public void onStart() {
				super.onStart();
				ActiveDetailActivity.this.dialog.show();
			}

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				ActiveDetailActivity.this.dialog.dismiss();
				MainToast.show(ActiveDetailActivity.this,
						ActiveDetailActivity.this.getResources().getString(R.string.netFail), 0);
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				ActiveDetailActivity.this.dialog.dismiss();
				ActiveDetailActivity.this.url_content = responseInfo.result.substring(
						responseInfo.result.lastIndexOf("<body"), responseInfo.result.lastIndexOf("</html>")).replace(
						" ", "");
				Log.e("cccccc", ActiveDetailActivity.this.url_content);
				ActiveDetailActivity.this.initData();
			}
		});
	}
}
